<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .container{
      display: flex;
    }
    .box{
      width: auto;
    }
    #hover-color, #click-color{
      width: 150px;
      height: 227px;
      border: 1px solid black;
      color: #fff;
      text-align: center;
      line-height: 227px;
      font-size: 12px;
    }
    p{
      text-align: center;
      font-weight: bold;
    }
  </style>
  <script>
    var canvas, ctx
    function draw() {
      canvas = document.getElementById('canvas')
      ctx = canvas.getContext('2d')

      canvas.addEventListener('mousemove', function(event) {
        pick(event, document.getElementById('hover-color'))
      })

      canvas.addEventListener('click', function(event) {
        pick(event, document.getElementById('click-color'))
      })

      var img = new Image()
      img.src = './rhino.jpg'
      img.onload = function() {
        ctx.drawImage(img, 0, 0, 300, 227)
      }
    }

    function pick(event, elm) {
      var x = event.layerX
      var y = event.layerY
      var pixel = ctx.getImageData(x, y, 2, 2)
      var data = pixel.data
      var rgba = `rgba(${data[0]}, ${data[1]}, ${data[2]}, ${data[3]})`
      elm.style.backgroundColor = rgba
      elm.innerText = rgba
    }
    
  </script>
</head>
<body onload="draw()">
  <div class="container">
    <div class="box">
      <p>source</p>
      <canvas id="canvas" width="300" height="227"></canvas>
    </div>
    <div class="box">
      <p>hover-color</p>
      <div id="hover-color"></div>
    </div>
    <div class="box">
      <p>click-color</p>
      <div id="click-color"></div>
    </div>
  </div>
</body>
</html>